<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use Hash;
use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class loginController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function login()
    {
        
        //用户登录页面
        return view('admin.login');
    }

    public function doLogin(Request $request)
    {
        $code = $request->get('code');
        $value = $request->session()->get('milkcaptcha');
        if($code==$value) {
            $username = $request->username;
            $user = User::where('username',$username)->first();

            //判断
            if(empty($user)){
                return back()->with('info','用户不存在'); 
            }

            $status = $user->status;
            if($status == '0'){
                return back()->with('info','用户没有权限');
            }
            //判断密码
            if(Hash::check($request->password,$user->password)){
                // session(['id'=>$user->id]);
                $request->session()->put('username', $user);
                return redirect('/admin')->with('info','登录成功');
            }else{
                return back()->with('info','密码错误');
            }
        }else{
            return back()->with('info','验证码错误');
        }

    }

    public function logout(Request $request)
    {
        $request->session()->flush();
        return redirect('/login');
    }


    
}
